package org.acooly.secretbox.ui;

import java.util.List;

import org.acooly.android.utils.ToastUtils;
import org.acooly.secretbox.R;
import org.acooly.secretbox.common.BaseActivity;
import org.acooly.secretbox.service.UserService;
import org.acooly.secretbox.service.domain.User;
import org.acooly.secretbox.service.impl.UserServiceImpl;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends BaseActivity {
	ProgressDialog progressDialog = null;
	List<String> listQuestions = null;
	private UserService userService;

	/**
	 * 多线程处理结果回传UI线程处理
	 */
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			String action = bundle.getString("action");
			if (action.equals("login")) {
				handleLoginResult(msg);
			} else if (action.equals("register")) {

			} else if (action.equals("forget")) {

			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		userService = new UserServiceImpl(this);
		switchView();
		// 登录
		Button buttonLogin = (Button) findViewById(R.id.login_submit);
		buttonLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				handleLogin(v);
			}
		});

		// 忘记密码
		Button buttonForget = (Button) findViewById(R.id.login_forget);
		buttonForget.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// LoginActivity.this.finish();
				Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
				startActivity(intent);
			}
		});

	}

	/**
	 * 处理登录
	 * 
	 * @param v
	 */
	private void handleLogin(View v) {
		progressDialog = ProgressDialog.show(LoginActivity.this, "", getResources().getString(R.string.login_login_progress), true);
		new Thread() {
			@Override
			public void run() {
				Message message = new Message();
				Bundle bundle = new Bundle();
				bundle.putString("action", "login");
				try {
					TextView passwordView = (EditText) findViewById(R.id.login_edt_password);
					String password = passwordView.getText().toString();

					if (password == null || password.equals("")) {
						throw new RuntimeException(getResources().getString(R.string.login_password_require));
					}
					User user = userService.get();
					if (!password.equals(user.getPassword())) {
						throw new RuntimeException(getResources().getString(R.string.login_password_failure));
					}
					bundle.putInt("result", 0);
				} catch (Exception e) {
					bundle.putInt("result", 1);
					bundle.putString("error", e.getMessage());
				} finally {
					progressDialog.dismiss();
					message.setData(bundle);
					handler.sendMessage(message);
				}
			}
		}.start();
	}

	/**
	 * 处理登录结果
	 * 
	 * @param message
	 */
	private void handleLoginResult(Message message) {
		Bundle bundle = message.getData();
		int result = bundle.getInt("result");
		if (result == 0) {
			LoginActivity.this.finish();
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			startActivity(intent);
		} else {
			((EditText) findViewById(R.id.login_edt_password)).setText("");
			ToastUtils.showShort(this, bundle.getString("error"));
		}
	}

	private void switchView() {
		boolean haveAlreadyRegistered = userService.haveAlreadyRegistered();
		Log.i("haveAlreadyRegistered", String.valueOf(haveAlreadyRegistered));

		if (!haveAlreadyRegistered) {
			this.finish();
			Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
			startActivity(intent);
		}
	}

}